package com.aboo.infrastructure.redis.frame.config;

import com.aboo.infrastructure.redis.frame.util.DistributedLockUtil;
import com.aboo.infrastructure.redis.frame.util.RedisUtils;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName RedisConfig
 * @Description
 * @Author louan
 * @Date 2024/6/28
 */
@Configuration
@EnableCaching
public class RedisConfig {

    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 设置键的序列化方式为字符串
        template.setKeySerializer(new StringRedisSerializer());
        // 设置哈希键的序列化方式为字符串
        template.setHashKeySerializer(new StringRedisSerializer());
        // 设置值的序列化方式，根据需要选择合适的序列化器
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 设置哈希值的序列化方式，同样根据需求选择
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return template;
    }

    @Bean
    public RedisUtils redisUtils() {
        return new RedisUtils();
    }

    @Bean
    public DistributedLockUtil distributedLockUtil() {
        return new DistributedLockUtil();
    }

}
